﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for email
/// </summary>
public class Email
{
    public Email(string emailAddress, string filePath)
    {
        this.EmailAddress = emailAddress;
        LoadXmlDocument(filePath);
    }

    public string EmailAddress { get; private set; }
    public string Name { get; private set; }
    public string To { get; private set; }
    public string Body { get; private set; }
    public string Subject { get; private set; }
    public string Bcc { get; private set; }
    public string Cc { get; private set; }
    public string From { get; private set; }
    public string ReplyTo { get; private set; }
    public bool Html { get; private set; }

    private void LoadXmlDocument(string filePath)
    {
        System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
        xmlDocument.Load(filePath);
        if (xmlDocument != null)
        {
            System.Xml.XmlNode subjectNode = xmlDocument.DocumentElement.SelectSingleNode("subject");
            if (subjectNode != null)
            {
                Subject = subjectNode.InnerText;
            }

            System.Xml.XmlNode bodyNode = xmlDocument.DocumentElement.SelectSingleNode("body");
            if (bodyNode != null)
            {
                Body = bodyNode.InnerText;
            }

            System.Xml.XmlNode bccNode = xmlDocument.DocumentElement.SelectSingleNode("bcc");
            if (bccNode != null)
            {
                Bcc = bccNode.InnerText;
            }

            System.Xml.XmlNode formatNode = xmlDocument.DocumentElement.SelectSingleNode("format");
            if (formatNode != null)
            {
                if (formatNode.InnerText != null && formatNode.InnerText.ToLower() == "html")
                    Html = true;
                else
                    Html = false;
            }

            System.Xml.XmlNode ccNode = xmlDocument.DocumentElement.SelectSingleNode("cc");
            if (ccNode != null)
            {
                Cc = ccNode.InnerText;
            }

            System.Xml.XmlNode fromNode = xmlDocument.DocumentElement.SelectSingleNode("from");
            if (fromNode != null)
            {
                From = fromNode.InnerText;
            }

            System.Xml.XmlNode replyToNode = xmlDocument.DocumentElement.SelectSingleNode("replyTo");
            if (replyToNode != null)
            {
                ReplyTo = replyToNode.InnerText;
            }
        }
    }
    
    public void Format(string name, string value)
    {
        string key = string.Format("[[{0}]]", name);
        if (Body.Contains(key))
            Body = Body.Replace(key, value);
        if (Subject.Contains(key))
            Subject = Subject.Replace(key, value);
    }

    public void Send()
    {
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

        if (!string.IsNullOrEmpty(EmailAddress))
        {
            mail.To.Add(new System.Net.Mail.MailAddress(EmailAddress, Name));
        }
        AddAddresses(this.Cc, mail.CC);
        AddAddresses(this.Bcc, mail.Bcc);
        if (!string.IsNullOrEmpty(this.From))
            mail.From = GetAddress(From);
        if (!string.IsNullOrEmpty(this.ReplyTo))
            mail.ReplyTo = GetAddress(From);

        mail.Subject = this.Subject;
        mail.Body = this.Body;

        
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        client.Send(mail);
        mail.Dispose();
               
    }

    private void AddAddresses(string addresses, System.Net.Mail.MailAddressCollection collection)
    {
        if (addresses != null)
        {
            foreach (string address in addresses.Split(';'))
            {
                collection.Add(GetAddress(address));
            }
        }
    }

    private System.Net.Mail.MailAddress GetAddress(string address)
    {
        if (address.Contains(","))
        {
            string email = address.Substring(0, address.IndexOf(","));
            string displayName = address.Substring(address.IndexOf(",") + 1);
            return new System.Net.Mail.MailAddress(email, displayName);
        }
        else
        {
            return new System.Net.Mail.MailAddress(address);
        }
    }

    public void AddBccAddress(string address)
    {
        if (!string.IsNullOrEmpty(Bcc) && !Bcc.EndsWith(";"))
        {
            Bcc = string.Concat(Bcc, ";");
        }
        Bcc = string.Concat(Bcc, address);
    }

    public void SetFrom(string address)
    {
        this.From = address;
    }
}
